// TODO set tpl cache.
// TODO more path setup and error process.
dojo.provide("lotex.report.Report");

dojo.require("dojo.lang.declare");
dojo.require("dojo.event.*");
dojo.require("lotex.system.Loader");
dojo.require("lotex.io");
dojo.require("lotex.system.AsynControler");
dojo.require("lotex.report.Dataset");
dojo.require("lotex.report.Constants");

// load report's schema and data
dojo.declare("lotex.report.Report", null,
    function(/*String : load path*/ uri) {
        if (uri) {
            this.uri = uri;
            this.loadFromUri(uri);
        }
        //this.sheet = {};
    },
{
    loadFromUri: function(uri) {
        // TODO self is necessary?
        var self = this;
        dojo.io.bind({
            url: uri,
            error: function(type, data, evt){
                d("无法生成报表：没有找到报表定义文件！");
            },
            load: function(type, define, evt) {
                self.loadReport(type, define, evt);
            },
            mimetype: "text/json"
        });
    },

    loadReport: function(type, define, evt) {
        this.define = define;
        var self = this;

        var loadingAsync = new lotex.system.AsynControler();
        loadingAsync.add(1); // for template loading
        loadingAsync.add(define.datasources.length);

        // datasource loading...

        this.datasources = {};
        dojo.lang.forEach(define.datasources, function(datasource){
            dojo.io.bind({
                url: lotex.system.Uri(datasource.uri),
                mimetype: "text/json",
                load: function(type, data, evt) {
                    self.datasources[datasource.name] =
                        new lotex.report.Dataset(data);
                    loadingAsync.remove(datasource.name);
                }
            });
        });

        // template loading
        var tpl = lotex.system.Loader.loadTemplate(define.templateUri);
        dojo.event.connect(tpl, "load", function(type, template, evt) {
            self.template = template;
            loadingAsync.remove('template');
        });

        dojo.event.connect(loadingAsync, "complete", function() {
            self.load();
        });
    },

    load: function() {},

    getColumn: function(idx,ds) {
        var col = this.getDataTable(ds).column;
        var i = dojo.lang.isNumber(idx) ? idx : this.getColumnIdxByName(idx);
        if (i<col.length) {
            return col[idx];
        }
        return null;
    },

    getColumnIdxByName: function(n, ds) {
        var col = this.getDataTable(ds).column;
        for (var i=0; i<col.length; i++) {
            if (col[i].name == n) return i;
        }
    },

    getDataset: function(name) {
        return this.getDatasource(name).dataset;
    },

    getDatasource: function(name) {
        var dsn = !name ? 'default' : name;
        var datasource = this.data.datasource;
        var len = datasource.length;
        for(var i=0;i<len;i++) {
            var ds = datasource[i];
            if(ds.name == dsn) return ds;
        }
    }
});